/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.app.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.market.base.app.mapper.entity.App;
import net.siufung.mybatis.base.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * <p>
 * 应用信息 Mapper 接口
 * </p>
 *
 * @author 陈建峰
 * @date 2023-01-12 16:24:13
 * @since v1.0.0
 */
@Mapper
public interface IAppMapper extends BaseMapper<App> {

    /**
     * 验证应用名称不能重复
     * @param verifyRepeatDto 重复
     * @return Boolean
     */
    default Boolean checkAppNameNotRepeat(VerifyRepeatDto<Integer> verifyRepeatDto){
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0 == this.selectCount(new LambdaQueryWrapper<App>()
                    .eq(App::getAppName, verifyRepeatDto.getVerifyKeyValue()));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0 == this.selectCount(new LambdaQueryWrapper<App>()
                    .eq(App::getAppName, verifyRepeatDto.getVerifyKeyValue())
                    .ne(App::getId, verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }

    /**
     * 验证应用编码不能重复
     * @param verifyRepeatDto 重复
     * @return Boolean
     */
    default Boolean checkAppCodeNotRepeat(VerifyRepeatDto<Integer> verifyRepeatDto){
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0 == this.selectCount(new LambdaQueryWrapper<App>()
                    .eq(App::getAppCode, verifyRepeatDto.getVerifyKeyValue()));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0 == this.selectCount(new LambdaQueryWrapper<App>()
                    .eq(App::getAppCode, verifyRepeatDto.getVerifyKeyValue())
                    .ne(App::getId, verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }

    /**
     * 验证应用客户端标识不能重复
     * @param verifyRepeatDto 重复
     * @return Boolean
     */
    default Boolean checkClientIdNotRepeat(VerifyRepeatDto<Integer> verifyRepeatDto){
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0 == this.selectCount(new LambdaQueryWrapper<App>()
                    .eq(App::getClientId, verifyRepeatDto.getVerifyKeyValue()));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0 == this.selectCount(new LambdaQueryWrapper<App>()
                    .eq(App::getClientId, verifyRepeatDto.getVerifyKeyValue())
                    .ne(App::getId, verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }
}
